package com.catering.controller;

import com.catering.common.core.domain.R;
import com.catering.domain.Customer;
import com.catering.domain.Vip;
import com.catering.mapper.CustomerMapper;
import com.catering.mapper.VipMapper;
import com.catering.service.VipService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:烟火味的男孩儿
 * 描述:一条寻找bug的道路
 * 时间:2024/10/18
 */
@RestController
@RequestMapping("/Vip")
public class VipController {
    @Autowired
    //注入service
    private VipService vipService;
    //注入vipMapper
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @GetMapping("/findAll")
    @PreAuthorize("@ss.hasPermi('catering:vip:list')")
//    @PreAuthorize("@ss.hasPermi('catering:vip:list')")
    public R findAll(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Customer> customers = vipService.queryAllVips();
        PageInfo<Customer> customerPageInfo = new PageInfo<>(customers);
        //查询数量放到pageInfo中
        long count = (long) vipService.getCustomerCount();
        customerPageInfo.setTotal(count);
        return R.ok(customerPageInfo);
    }

    //根据拿到的vip的ID删除该条数据
    @DeleteMapping("/deleteById/{eid}")
    @PreAuthorize("@ss.hasPermi('catering:vip:list')")
    public R deleteById(@PathVariable Long eid) {
        //删掉对应的顾客信息和vip信息
        vipService.deleteByPrimaryKey(eid);
        return R.ok(customerMapper.deleteByPrimaryKey(eid));
    }

    //根据拿到的vip的ID修改该条数据
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('catering:vip:list')")
    public R updateById(@RequestBody Customer customer) {
        int money = customer.getStorageMoney();
        Vip vip = vipMapper.selectByPrimaryKey((long) customer.getVip().getId());
        if (customer.getStorageMoney() > 0 && customer.getStorageMoney() < 2000) {
            vip.setVtype(1);
        } else if (customer.getStorageMoney() <= 5000) {
            vip.setVtype(2);
        } else if (customer.getStorageMoney() <= 10000) {
            vip.setVtype(3);
        } else {
            vip.setVtype(4);
        }
        vipMapper.updateByPrimaryKey(vip);
        return R.ok(vipService.updateByPrimaryKey(customer));
    }

    //查询单个vip信息
    @GetMapping("/findVipById/{eid}")
    @PreAuthorize("@ss.hasPermi('catering:vip:list')")
    public R findById(@PathVariable("eid") Long eid) {
        return R.ok(vipService.getCustomerById(eid));
    }

    //查询单个vip信息
    @GetMapping("/queryVipById/{eid}")
    @PreAuthorize("@ss.hasPermi('catering:vip:list')")
    public R queryById(@PathVariable("eid") Long eid) {
        Customer customer = vipService.getCustomerById(eid);
        ArrayList<Customer> list = new ArrayList<>();
        list.add(customer);
        return R.ok(list);
    }

    //添加会员的方法
    @PostMapping("/addVip/{id}")
    @PreAuthorize("@ss.hasPermi('catering:vip:list')")
    public R addVip(@RequestBody Vip vip, @PathVariable("id") Integer eid) {
        System.out.println(eid);
        //将顾客的vid设置为id
        Customer customer = customerMapper.queryCustomer((long) eid);
        customer.setVipId(eid);
        customerMapper.updateByPrimaryKey(customer);
        //替换自动生成的id
        vip.setId(eid);
        //添加到数据库
        return R.ok(vipService.addCustomerToVip(vip));
    }

    @PreAuthorize("@ss.hasPermi('catering:vip:list')")
    @GetMapping("/all")
    public R queryCustomers() {
        //查询所有的顾客信息
        return R.ok(customerMapper.queryAllCustomers());
    }
}
